import { createRouter, createWebHistory } from "vue-router";
import Index from "./components/Index.vue";
import Login from "./components/Login.vue";
const routes = [
  {
    path: "/",
    redirect: "/login",
  },
  {
    path: "/login",
    component: Login,
    meta: { role: 200 },
  },
  {
    path: "/index",
    component: Index,
    children: [
      {
        path: "/apply/account",
        component: () => import("./views/register/registerApply.vue"),
        meta: { title: "注册申请", role: 2 },
      },
      {
        path: "/apply/drugs",
        component: () => import("./views/drugs/drugsApply.vue"),
        meta: { title: "药品申请", role: 2 },
      },
      {
        path: "/notice",
        component: () => import("./views/notice/noticeManage.vue"),
        meta: { title: "公告管理", role: 2 },
      },
      {
        path: "/checkup",
        component: () => import("./views/check/checkDetails.vue"),
        meta: { title: "体检信息", role: 2 },
      },
    ],
  },
  {
    path: "/403",
    component: () => import("../src/components/illegal/403.vue"),
    meta: { role: 200 },
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes, // `routes: routes` 的缩写
});
router.beforeEach((to, from, next) => {
  //to 是到达的路由
  //from 是来源的路由信息
  //next 是帮助我们跳转路由的函数
  if (to.path === "/login") {
    localStorage.clear();
    next();
  } else {
    setTimeout(function () {
      let role = localStorage.getItem("role");
      if (to.meta.role == 200) {
        next();
      } else if (role != to.meta.role) {
        next("/403");
      } else {
        next();
      }
    }, 200);
  }
});
export default router;
